package com.cntcdt.api.oauth;

import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;

public class RequestTokenResource extends ServerResource {
    private String consumer_secret = "MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98";
    private String oauth_callback = "";
    private String oauth_consumer_key = "GDdmIQH6jhtmLUypg82g";
    private String oauth_nonce = "QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk";
    private String oauth_signature_method = "HMAC-SHA1";
    private String oauth_timestamp;
    private String oauth_version = "1.0";
    private Request request;

    public RequestTokenResource() {
    }

    @Post
    public Representation post(Representation entity) {
	Form requestHeaders = (Form) Request.getCurrent().getAttributes()
		.get("org.restlet.http.headers");
	this.oauth_consumer_key = requestHeaders
		.getFirstValue("oauth_consumer_key");
	this.consumer_secret = requestHeaders.getFirstValue("consumer_secret");
	this.oauth_signature_method = requestHeaders
		.getFirstValue("oauth_signature_method");
	this.oauth_callback = requestHeaders.getFirstValue("oauth_callback");
	if (this.oauth_consumer_key == null || this.consumer_secret == null) {
	    Response.getCurrent().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
	    return null;
	} else {
	    // 使用oauth_consumer_key查询APP信息
	    Form responseHeaders = (Form) getResponse().getAttributes().get(
		    "org.restlet.http.headers");
	    if (responseHeaders == null) {
		responseHeaders = new Form();
		getResponse().getAttributes().put("org.restlet.http.headers",
			responseHeaders);
	    }
	    responseHeaders.add("oauth_token", "XXXXXXXXXx-test"); // 这里需要计算生成oauth_token;
	    responseHeaders.add("oauth_token_secret", "secretXXXXXXXXXx-test"); // 这里需要计算生成oauth_token;
	    responseHeaders.add("oauth_callback_confirmed", "true");
	}
	Representation rep = new StringRepresentation("XXXXXXXXX");
	rep.setMediaType(MediaType.TEXT_PLAIN);
	return null;
    }
}
